//app.js 
App({
  onLaunch: function () {

    // 展示本地存储能力 
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    let _this = this;

    const { model, system, statusBarHeight } = wx.getSystemInfoSync();
    var headHeight;
    if (/iphone\s{0,}x/i.test(model)) {
      headHeight = 88;
    } else if (system.indexOf('Android') !== -1) {
      headHeight = 68;
    } else {
      headHeight = 64;
    }
    this.globalData.headerHeight = headHeight;
    this.globalData.statusBarHeight = statusBarHeight;

  },


  onShow: function () {

  },

  getRequest: function (requrl, data) {
    var _this = this;
    return new Promise(function (resolve, reject) {
      var resstr = "";
      wx.request({
        url: _this.data.service_url + requrl,
        method: "GET",
        data: data,
        success: function (res) {
          resolve(res);
        },
        fail: function (res) {
          wx.showToast({
            title: '请求失败',
            icon: 'none',
            duration: 3000,
            mask: false
          })
        }
      })
    })
  },

  paiRequest: function (requrl, data) {
    var _this = this;
    // wx.showLoading({ 
    //   title: '加载数据', 
    //   icon: 'loading', 
    // }) 
    var openid = _this.data.openid;
    if (openid == undefined) {

    } else {
      try {
        var userInfo = this.globalData.userInfo;
        var userId = userInfo.id;
        if (userId != undefined) {
          data.userId = userId;
        }
      } catch (e) {
      }
    }

    return new Promise(function (resolve, reject) {
      var resstr = "";
      wx.request({
        url: _this.data.service_url + requrl,
        data: data,
        async: false,
        method: "POST",
        header: { "content-type": 'application/x-www-form-urlencoded' },
        success: function (res) {
          var code = res.data.code;
          if (code == "200") {
            resolve(res.data);
          } else {
            var message = res.data.message;
            wx.showToast({
              title: message,
              icon: 'none',
              duration: 3000,
              mask: false
            })
          }
        },
        fail: function (res) {
          var datastr = JSON.stringify(data);
          wx.showToast({
            title: '5请求失败' + datastr,
            icon: 'none',
            duration: 3000,
            mask: false
          })
        }
      })
      //   wx.hideLoading(); 
    })
  },

  uploadFiles: function (files) {
    var _this = this;

    return new Promise(function (resolve, reject) {
      var topicImg = "";
      for (let i = 0; i < files.length; i++) {
        var file = files[i];
        wx.uploadFile({
          url: _this.data.service_url + '/app/uploadfile',
          filePath: file,
          name: 'file',
          success: function (res) {
            var data = JSON.parse(res.data);
            var code = data.code;
            var arry = data.data;
            console.log(topicImg);
            if (code == "200") {
              if (topicImg.length > 0) {
                topicImg = topicImg + "|";
              }
              topicImg = topicImg + arry[0];
              console.log(topicImg);
            } else {
              var message = data.message;
              wx.showToast({
                title: message,
                icon: 'none',
                duration: 3000,
                mask: false
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '请求失败',
              icon: 'none',
              duration: 3000,
              mask: false
            })
          }
        })
      }
      resolve(topicImg);
    });
  },
  resetHistoryList: function (item) {
    //console.info('item', item) 
    var that = this
    //let historyObj = that.globalData.localStorage.usage.history; 
    let newList = [];
    for (let idx in that.globalData.localStorage.usage.history) {
      let model = that.globalData.localStorage.usage.history[idx];
      //console.info('model', model) 
      if (model.bookname != item.bookname) {
        newList.push(model);
      }
    }
    newList.push(item);
    //console.info('newList', newList) 
    let length = newList.length;
    if (length > 5) {
      newList = newList.slice(length - 5, length);
    }
    that.globalData.localStorage.usage.history = newList;
  },
  getHistorListOrderByDesc: function () {
    var that = this
    let newList = [];
    let length = that.globalData.localStorage.usage.history.length;
    for (let idx in that.globalData.localStorage.usage.history) {
      let model = that.globalData.localStorage.usage.history[length - idx - 1];
      newList.push(model);
    }
    return newList;
  },
  globalData: {
    userInfo: null,
    headerHeight: 0,
    statusBarHeight: 0,
    audFlag: false,
    localStorage: {
      style: {},
      usage: {
        history: []
      }
    },
    localStorageKey: 'miaomiao_localStorage_key',
    shareSetting: {
      title: '执行教育',
      path: '/pages/index/index'
    },
    imgbaseurl: ''
  },
  data: {
    service_url: "https://www.bjhylfkj.com/knowing/",
    server: "https://www.bjhylfkj.com",
    aliyunOss: 'http://bjhylfkj.oss-cn-beijing.aliyuncs.com/',
    code: '',
    openid: '',
    session_key: '',
    encryptedData: '',
    iv: ''
  }
})